package com.cn.bukeManage.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cn.bukeManage.pojo.User;
import com.cn.bukeManage.pojo.page.Page;
import com.cn.bukeManage.service.UserService;
import com.cn.bukeManage.utils.MD5Utils.MD5Utils;

@Controller
@RequestMapping("/login")
public class UserController {
	@Resource
	private UserService userService;

	/**
	 * 登录页面
	 * @return
	 */
	@RequestMapping("/toLogin")
	public String toLogin() {
		return "/login";
	}
	
	//账号管理页面
	@RequestMapping("/toAccountManage")
	public String toAccountManage(int currentPage, Model model,HttpServletResponse res){
		res.setHeader("Access-Control-Allow-Origin", "*");  
	    res.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");  
	    res.setHeader("Access-Control-Max-Age", "3600");  
	    res.setHeader("Access-Control-Allow-Headers", "x-requested-with");
		Page page = userService.findAllUsers(currentPage, model);
		model.addAttribute("page",page);
		return "/account/accountManage";
	}
	
	
	@RequestMapping("/toModifyPwd")
	public String toModifyPwd(){
		return "/account/modifyPwd";
	}
	
	
	@RequestMapping(value="/modifyPwd",method = RequestMethod.POST)
	public String modifyPwd(HttpSession session,Model model,String yuanPwd,String newPwd){
		//1.验证密码的正确性
		User currentUser=userService.get(Integer.valueOf(String.valueOf(SecurityUtils.getSubject().getSession().getAttribute("userId"))));
		if(currentUser !=null){
			 User user=userService.checkUser(currentUser.getUserName(),  yuanPwd);
			 if(user==null){
				 model.addAttribute("msg", "请检查原密码...");
				return "/account/modifyPwd";
			 }
			//2,密码正确，修改密码
			 String salt=user.getWeiyi();
			 String encPwd=MD5Utils.GetMD5Code(newPwd.concat(salt));
			 user.setPasswd(encPwd);
			 userService.updateUser(user);
		}
		//注销重新登录
		 return logout(session);
	}
	
	@RequestMapping("blockUp")
	public Integer blockUp(Integer userId){
		return userService.blockUp(userId);
	}
	
	/**
	 * 注销
	 * @param session
	 * @return
	 */
	@RequestMapping("logout")
	public String logout(HttpSession session) {
		SecurityUtils.getSubject().logout();
		return "redirect:/";
	}
	
	@RequestMapping("myWallet")
	@ResponseBody
	public Double myWallet(Model model){
		return userService.getMyWallet(String.valueOf(SecurityUtils.getSubject().getSession().getAttribute("userId")));
	}
	
	/**
	 *  首页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/toWelcome")
	public String toIndex(Model model) {
		model.addAttribute("oneMenu", "welcome");
		return "index/welcome";
	}

	/**
	 * 身份验证
	 * @param user
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "checkLogin",method = RequestMethod.POST)
	public String checkLogin(User user, Model model) {
		UsernamePasswordToken token = new UsernamePasswordToken(user.getUserName(), user.getPasswd(), null);
		Subject currentUser = SecurityUtils.getSubject();
		try {
			currentUser.login(token);
			return "/index/welcome";
		} catch (Exception e) {
			// TODO: handle exception
			token.clear();
			return "redirect:/";
		}
	}
	
	// 跳转到提现页面
	@RequestMapping("/withDrawalMoney")
	public String repair(Model model) {
		model.addAttribute("oneMenu", "wallet");
		model.addAttribute("twoMenu", "withDrawal");
		return "/wallet/withDrawalMoney";
	}


}
